




package com.yinhai.ta404.core.validate.validator;

import com.yinhai.ta404.core.autoconfigure.TaCustomRegexProperties;
import com.yinhai.ta404.core.autoconfigure.TaRegexMessageProperties;
import com.yinhai.ta404.core.validate.annotation.V;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class AbstractPatternValidator<T> implements ConstraintValidator<V, T> {
    protected String[] conditions;
    protected Class<?> type = null;
    @Autowired
    TaCustomRegexProperties validationConfigProperties;

    public AbstractPatternValidator() {
    }

    public void initialize(final V constraintAnnotation) {
        this.conditions = constraintAnnotation.value();
        this.type = constraintAnnotation.type();
    }

    protected TaRegexMessageProperties getMessageRegex(String kee) {
        return (TaRegexMessageProperties)this.validationConfigProperties.getCustomRegex().getOrDefault(kee, new TaRegexMessageProperties());
    }

    protected boolean hasCustomRegex(String kee) {
        return this.validationConfigProperties.getCustomRegex().containsKey(kee);
    }

    public abstract boolean isValid(T input, ConstraintValidatorContext context);
}
